package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.entity.DishFlavor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品同时保存口味数据
     *
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到菜品表dish
        this.save(dishDto);

        Long dishId = dishDto.getId();//菜品id

        //菜品口味
        List<DishFlavor> dishFlavors = dishDto.getFlavors();
        dishFlavors = dishFlavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //  dishFlavors.forEach(s->s.setDishId(dishId));

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(dishFlavors);
    }

    /**
     * 根据id查询菜品和对应的口味信息
     * @param id
     * @return
     */
    public DishDto getByIdWithFlavor(Long id) {
        //查询dish表
        Dish dish = this.getById(id);

        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dish, dto);

        //查询dish_flavor
        Long dishId = dish.getId();
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dishId != null, DishFlavor::getDishId, dishId);
        lqw.orderByDesc(DishFlavor::getUpdateTime);
        List<DishFlavor> list = dishFlavorService.list(lqw);

        dto.setFlavors(list);

        return dto;
    }
    /**
     * 更新菜品和对应的口味信息
     * @param dishDto
     * @return
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);

        //清理当前菜品对应口味数据
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        List<DishFlavor> flavors = dishDto.getFlavors();
        Long dishId = dishDto.getId();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //添加当前提交的口味数据
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 删除菜品和口味信息
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithFlavor(Long ids) {
        Dish dish = this.getById(ids);
        if(dish.getStatus()==1){
            throw new CustomException("菜品起售不能删除");
        }else {
            this.removeById(ids);

            LambdaQueryWrapper<DishFlavor> lqw=new LambdaQueryWrapper<>();

            lqw.eq(DishFlavor::getDishId,ids);

            dishFlavorService.remove(lqw);
        }
    }


}
